package com.jetpack.mvvm.lib.common.ext

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import kotlin.reflect.KProperty1

fun <T> LifecycleOwner.observe(liveData: LiveData<T>, action: (t: T) -> Unit) {
    liveData.observe(this) {
        it?.let {
            action(it)
        }
    }
}

fun <T, A> LifecycleOwner.observeState(
    liveData: LiveData<T>,
    prop1: KProperty1<T, A>,
    action: (A) -> Unit
) {
    liveData.observeState(this, prop1, action)
}

fun <T, A> LifecycleOwner.observeState(
    liveData: LiveData<T>,
    prop1: KProperty1<T, A>,
    expected: A? = null,
    action: (A) -> Unit
) {
    liveData.observeState(this, prop1, expected, action)
}